//
//  Timeline.swift
//  01sinablog
//
//  Created by ZhangZiang on 15/12/18.
//  Copyright © 2015年 ZhangZiang. All rights reserved.
//

import UIKit

class Timeline: NSObject {

    var created_at : String?
    
    var text : String?
    
    var source : String?
    
    //原创微博发布者,用模型
    var user : User?
    
//    var retweeted_status : [String : AnyObject]?
    
    var id : Int64 = 0
    
    var pic_urls : [[String : String]]? {
        //当图片id数组被赋值时候,就都转成url数组
        didSet{
            guard let array = pic_urls else
            {
                return
            }
            //实例化数组,然后再拼接才有用
            imageURLs = [NSURL]()
            for dict in array
            {
                let urlString = dict["thumbnail_pic"]!
                
                imageURLs!.append(NSURL(string: urlString)!)
            }
        }
    }
    //url数组
    var imageURLs : [NSURL]?
    
    init(dict : [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    var retweeted_status : Timeline?
    
    override func setValue(value: AnyObject?, forKey key: String) {
        //如果是user,则字典转模型
        if key == "user"
        {
            if let dict = value as? [String : AnyObject]
            {
            user = User(dict : dict)
                return
            }
//            print(user)
            //这里一定要return,要不上面字典转模型就白做了
        }
        
        if key == "retweeted_status"
        {
            if let dict = value as? [String : AnyObject]
            {
                retweeted_status = Timeline(dict: dict)
                return
            }
        }
        //给其他属性赋值
        super.setValue(value, forKey: key)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
}
